using System;
using System.Collections;
using System.IO;
using System.Text;

namespace Devsoft.Utilities
{
	/// <summary>
	/// Summary description for DictParser.
	/// </summary>
	public class DictParser
	{
		//Variable Member
		protected String _IndexFile = String.Empty;
		protected String _DictFile = String.Empty;

		//Properties
		public string IndexFile
		{
			get
			{
				return _IndexFile;
			}
			set
			{
				_IndexFile = value;
			}
		}

		public string DictFile
		{
			get
			{
				return _DictFile;
			}
			set
			{
				_DictFile = value;
			}
		}
		public DictParser()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		public String GetMeanings(DictEntry aEntry)
		{
			FileStream fs = new FileStream(_DictFile, FileMode.Open, FileAccess.Read);
			fs.Seek(aEntry.Offset,SeekOrigin.Begin);
			Byte[] tmp = new Byte[aEntry.Length];
			fs.Read(tmp,0,aEntry.Length);
			fs.Close();	
			UTF8Encoding uEncode = new UTF8Encoding();			
			return uEncode.GetString(tmp,0,tmp.Length);
		}
		public DictEntry[] LoadIndex() 
		{
			ArrayList arrTemp = new ArrayList();
            
			StreamReader sr = new StreamReader(_IndexFile,new System.Text.UTF8Encoding());
			String sTemp =  String.Empty;
			while (sr.Peek() > 0)
			{
				sTemp = sr.ReadLine();
				arrTemp.Add(new DictEntry(sTemp));
			}//End While
			sr.Close();

			//Fetching the results
			DictEntry[] arrResult = new DictEntry[arrTemp.Count];
			for (int i = 0; i < arrTemp.Count ; i++)
			{
				arrResult[i] = (DictEntry)(arrTemp[i]);
			}//End For
			return arrResult;
		}
	}
}
